﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GetDLLBit
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;

                if(GetPEArchitecture(openFileDialog1.FileName) == 0x10B)
                    MessageBox.Show("32bit","File bit",MessageBoxButtons.OK,MessageBoxIcon.Information);
                else if (GetPEArchitecture(openFileDialog1.FileName) == 0x20B)
                    MessageBox.Show("64bit", "File bit", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show("Unknown", "File bit", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        public static ushort GetPEArchitecture(string filePath)
        {
            ushort architecture = 0;
            try
            {
                using (System.IO.FileStream fStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    using (System.IO.BinaryReader bReader = new System.IO.BinaryReader(fStream))
                    {
                        if (bReader.ReadUInt16() == 23117)
                        {
                            fStream.Seek(0x3A, System.IO.SeekOrigin.Current);
                            fStream.Seek(bReader.ReadUInt32(), System.IO.SeekOrigin.Begin);
                            if (bReader.ReadUInt32() == 17744)
                            {
                                fStream.Seek(20, System.IO.SeekOrigin.Current);
                                architecture = bReader.ReadUInt16(); 
                    }
                        }
                    }
                }
            }
            catch { }
            return architecture;
        }
    }
}
